package vn.gq.tym.widget;

import android.content.Context;
import android.util.AttributeSet;

public class WebView extends android.webkit.WebView {

	private int scrollY = 0;
	private int scrollX = 0;
	private int scrolled = 0;
	
	public WebView(Context context) {
		super(context);
	}
	
	public WebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public WebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void setScrollTo(int x, int y) {
		scrollX = x;
		scrollY = y;
	}

	@Override
	public void invalidate() {
		super.invalidate();
		if (getContentHeight() > 0 && ++scrolled <= 2) {
			scrollTo(scrollX, scrollY);
		}
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (scrollListener != null) {
			scrollListener.onScrollChange(l, t);
		}
	}
	
	private OnScrollListener scrollListener;
	
	public void setScrollListener(OnScrollListener listener) {
		scrollListener = listener;
	}
	
	public interface OnScrollListener {
		public void onScrollChange(int x, int y);
	}
}
